package org.example.huweb.web.sys_login.controller;

import cn.hutool.core.bean.BeanUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.Result.Result;
import org.example.Result.ResultUtils;
import org.example.Result.StatusCode;
import org.example.dto.UserLoginDTO;
import org.example.entity.UserRole;
import org.example.exception.BusinessException;
import org.example.huweb.web.sys_login.service.impl.UserRoleServiceImpl;
import org.example.vo.UserReportVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Objects;


@RestController

@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class userController {
    private final UserRoleServiceImpl userRoleService;

    //验证用登录
    @PostMapping("/loginPost")
    public Result verifyUserLogin(String email, String password) {
        log.info("用户登录{}{}", email, password);
        if (Objects.isNull(email) || Objects.isNull(password) || email.isEmpty() || password.isEmpty()) {
            throw new BusinessException(StatusCode.ERROR_CODE, "用户名称或密码不能为空");
        }
        UserReportVO login = userRoleService.login(email, password);

        return ResultUtils.success("登录成功", login);
    }

    //用户注册
    @PostMapping("/enrollUserPost")
    public Result adduser(@RequestBody UserLoginDTO user) {
        UserRole userRole = BeanUtil.copyProperties(user, UserRole.class);
        userRole.setMyBirthday(LocalDateTime.now());
        userRole.setMyAvatar("@/components/icons/avatar.png");
        userRoleService.adduser(userRole);
        return ResultUtils.success("ok");
    }

}
